import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { DishService } from '@/services/dishService';

// GET /api/dishes/[id]
export async function GET(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const dish = await DishService.getDishById(params.id);
        if (!dish) {
            return NextResponse.json(
                { error: '菜品不存在' },
                { status: 404 }
            );
        }
        return NextResponse.json(dish);
    } catch (error) {
        return NextResponse.json(
            { error: '获取菜品失败' },
            { status: 500 }
        );
    }
}

// PUT /api/dishes/[id]
export async function PUT(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        const data = await request.json();
        const updatedDish = await DishService.updateDish(params.id, data);
        return NextResponse.json(updatedDish);
    } catch (error) {
        console.error('Update dish error:', error);
        return NextResponse.json(
            { error: '更新菜品失败' },
            { status: 500 }
        );
    }
}

// DELETE /api/dishes/[id]
export async function DELETE(
    request: NextRequest,
    { params }: { params: { id: string } }
) {
    try {
        await DishService.deleteDish(params.id);
        return NextResponse.json({ success: true });
    } catch (error) {
        return NextResponse.json(
            { error: '删除菜品失败' },
            { status: 500 }
        );
    }
} 